package com.itheima.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.ProductService;
import com.itheima.mapper.InventoryMapper;
import com.itheima.mapper.ProductMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    /*
     *   查询所有商品分类
     * */
    @Override
    public PageBean categoriesList(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Category> categoryList = productMapper.getcategoriesList(page, pageSize, name);
        Page<Category> c = (Page<Category>) categoryList;
        PageBean pageBean = new PageBean(c.getTotal(), c.getResult());
        return pageBean;
    }

    //    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        Integer start = (page - 1) * pageSize;
//        // 获取总页数
//        List<Product> productList = productMapper.page(start, pageSize);
//        // 获取总记录数
//        Long count = productMapper.count();
//        // 封装分页查询数据
//        PageBean pageBean = new PageBean(count, productList);
//        return pageBean;
//    }
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer category_id, LocalDate begin, LocalDate end) {
        //  设置分页参数
        PageHelper.startPage(page, pageSize);
        //  执行查询
        List<Product> productList = productMapper.list(name, category_id, begin, end);
        // 强转化为page类型
        Page<Product> p = (Page<Product>) productList;

        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

        return pageBean;
    }

    @Override
    public void deleteCategoryById(List<Integer> ids) {
        productMapper.deleteCategoryById(ids);
    }

    @Override
    public void deleteProductById(List<Integer> ids) {
        productMapper.deleteProductByIds(ids);
    }

    @Override
    public void addProduct(Product product) {
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        productMapper.addProduct(product);
        inventoryMapper.addInventory(product.getCategoryId());
    }

    @Override
    public Product getProduct(int id) {
        return productMapper.getProduct(id);
    }

    /*
     *   更新商品
     * */
    @Override
    public void updateProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        productMapper.updateProduct(product);
    }

    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        productMapper.updateCategory(category);
    }

    @Override
    public void insertCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        productMapper.addCategory(category);
    }

    @Override
    public Category selectCategoryById(int id) {
        return productMapper.selectCategoryById(id);
    }

    @Override
    public Product validate(String name) {
        return productMapper.validate(name);
    }

}
